package com.heer.gymnasium.module.main.views;

import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseFragment;
import com.heer.gymnasium.base.BaseRefreshFragment;
import com.heer.gymnasium.module.advise.beans.AdMainBean;
import com.heer.gymnasium.module.article.beans.ArticleBean;
import com.heer.gymnasium.module.article.beans.ArticleResponseBean;
import com.heer.gymnasium.module.article.views.ArticleActivity;
import com.heer.gymnasium.module.main.adapters.HomeAdapter;
import com.heer.gymnasium.module.main.beans.BannerResponseBean;
import com.heer.gymnasium.util.StringUtils;
import com.heer.imagepick.DensityUtils;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.zhihuiyun.library.bannar.Banner;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan on 2018/1/11.
 * 发现
 */

public class FindFragment extends BaseRefreshFragment {

    private final static int RES_ID_ROOT = R.layout.layout_find;
    private final static int RES_ID_NEWS_CB = R.id.layout_find_news_cb;
    private final static int RES_ID_NEWS_FL = R.id.layout_find_news_fl;
    private final static int RES_ID_ACT_CB = R.id.layout_find_act_cb;
    private final static int RES_ID_ACT_FL = R.id.layout_find_act_fl;

    private final static int RES_ID_BANNER_PANEL = R.id.layout_find_banner_ll;
    private final static int RES_ID_BANNER_LEFT = R.id.layout_find_arrow_left_fl;
    private final static int RES_ID_BANNER_RIGHT = R.id.layout_find_arrow_right_fl;
    private final static int RES_ID_BANNER = R.id.layout_find_banner;

    private final static int RES_ID_GRID = R.id.layout_find_gv;
    private final static int RES_ID_GRID_ITEM = R.layout.item_article;
    private final static int RES_ID_GRID_ITEM_BG = R.id.item_article_ad_iv;

    public final static int ARTICLE_TYPE_NEWS = 1;
    public final static int ARTICLE_TYPE_ACT = 2;
    private final static String BANNER_TYPE_NEWS = "3";
    private final static String BANNER_TYPE_ACT = "2";


    @BindView(RES_ID_NEWS_CB)
    CheckBox cbNews;
    @BindView(RES_ID_ACT_CB)
    CheckBox cbAct;
    @BindView(RES_ID_BANNER_PANEL)
    View bannerView;
    @BindView(RES_ID_BANNER_LEFT)
    View bannerLeft;
    @BindView(RES_ID_BANNER_RIGHT)
    View bannerRight;
    @BindView(RES_ID_BANNER)
    Banner banner;
    @BindView(RES_ID_GRID)
    GridView gridView;
    @BindView(RES_ID_REFRESH_LAYOUT)
    SwipyRefreshLayout refreshLayout;

    private ArrayList<ArticleBean> articleList;
    private QuickTypeAdapter<ArticleBean> articleAdapter;
    private int currentType;

    @Override
    protected int setConvertView() {
        return RES_ID_ROOT;
    }

    @Override
    protected void init() {
        super.init();
        currentType = ARTICLE_TYPE_NEWS;
        articleList = new ArrayList<>();
        articleAdapter = new QuickTypeAdapter<ArticleBean>(getContext(),articleList,RES_ID_GRID_ITEM) {
            @Override
            public void bindData4View(ViewHolder holder, final ArticleBean data, int type, int pos) {
                HomeAdapter.bindArticleItem(context,holder,data,pos,currentType);
            }
        };
    }

    @Override
    protected void initView() {
        super.initView();
        refreshLayout.setOnRefreshListener(this);
        gridView.setHorizontalSpacing(DensityUtils.dip2px(getContext(),5));
        gridView.setVerticalSpacing(DensityUtils.dip2px(getContext(),15));
        gridView.setAdapter(articleAdapter);
        gridView.setNumColumns(2);
    }

    @Override
    protected void loadData(boolean loadMore) {
        super.loadData(loadMore);
        getBannerList(currentType == ARTICLE_TYPE_ACT ? BANNER_TYPE_ACT : BANNER_TYPE_NEWS);
        if (!loadMore){
            articleList.clear();
        }
        loadArticleData();
    }

    @OnClick({RES_ID_NEWS_CB,RES_ID_NEWS_FL})
    public void showNewsList(){
        cbNews.setChecked(true);
        cbAct.setChecked(false);
        currentType = ARTICLE_TYPE_NEWS;
        svProgressHUD.showWithStatus("加载中...");
        loadData();
    }

    @OnClick({RES_ID_ACT_CB,RES_ID_ACT_FL})
    public void showActList(){
        cbNews.setChecked(false);
        cbAct.setChecked(true);
        currentType = ARTICLE_TYPE_ACT;
        svProgressHUD.showWithStatus("加载中...");
        loadData();
    }

    private void loadArticleData(){
        mAppHelper.apiService().getArticleList(String.valueOf(currentType),null,String.valueOf(pageNo)).enqueue(new BaseCallback<ApiResponse<ArticleResponseBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<ArticleResponseBean>> call, Response<ApiResponse<ArticleResponseBean>> response) {
                initArticleList(ArticleBean.dataChange(response.body().getData().getData()));
            }
        });
    }

    private void initArticleList(List<ArticleBean> articleBeans) {
        articleList.addAll(articleBeans);
        if (currentType == ARTICLE_TYPE_NEWS){
            gridView.setNumColumns(2);
        }else{
            gridView.setNumColumns(1);
        }

        articleAdapter.notifyDataSetChanged();
        if (svProgressHUD.isShowing()){
            svProgressHUD.dismissImmediately();
        }
    }

    //获取广告
    private void getBannerList(String bannerType) {
        mAppHelper.apiService().getBannerList(bannerType).enqueue(new BaseCallback<ApiResponse<List<BannerResponseBean>>>() {
            @Override
            public void onSuccess(Call<ApiResponse<List<BannerResponseBean>>> call, Response<ApiResponse<List<BannerResponseBean>>> response) {
                setRefresh(false);
                if (response.isSuccessful() && response.body().isSccessful()) {
                    initBanner(AdMainBean.changeFromBannerList(response.body().getData()));
                }
            }

            @Override
            public void onError(Call<ApiResponse<List<BannerResponseBean>>> call, Throwable t) {
                setRefresh(false);
                ToastUtil.show(mContext, "获取失败");
            }
        });
    }

    private void initBanner(List<AdMainBean> data) {
        HomeAdapter.bindBannerItem(bannerView, Glide.with(getContext()),banner,bannerLeft,bannerRight,data);
    }


    private void setRefresh(boolean refresh) {
        if (refreshLayout != null){
            refreshLayout.setRefreshing(refresh);
        }
    }

}
